package com.suyunyou.manager.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.suyunyou.manager.dao.SpiderLinkDao;
import com.suyunyou.manager.pojo.SpiderLink;
import com.system.comm.enums.Boolean;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameMd5Util;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;

/**
 * 提取链接的Service
 * @author yuejing
 * @date 2016年6月26日 上午8:31:45
 * @version V1.0.0
 */
@Component
public class SpiderLinkService {

	private static final Logger LOGGER = LoggerFactory.getLogger(SpiderLinkService.class);
	@Autowired
	private SpiderLinkDao spiderLinkDao;
	
	/**
	 * 新增
	 * @param link
	 * @param domain
	 * @param siteId
	 */
	public synchronized void save(String link, String domain, Integer siteId) {
		SpiderLink sl = getLink(link);
		if(sl == null) {
			sl = new SpiderLink();
			sl.setLink(link);
			sl.setMd5Link(getMd5Link(link));
			sl.setDomain(domain);
			sl.setSiteId(siteId);
			sl.setIsFetcherContent(Boolean.FALSE.getCode());
			sl.setIsFetcherLink(Boolean.FALSE.getCode());
			try {
				spiderLinkDao.save(sl);
			} catch (Exception e) {
				LOGGER.error("保存页面出现异常: " + e.getMessage());
			}
		}
	}

	private String getMd5Link(String link) {
		return FrameMd5Util.getInstance().encodePassword(link);
	}

	/**
	 * 根据爬取地址获取对象
	 * @param link
	 * @return
	 */
	public SpiderLink getLink(String link) {
		return spiderLinkDao.getLink(getMd5Link(link));
	}

	/**
	 * 根据链接修改内容
	 * @param link
	 * @param content
	 */
	public void updateContent(String link, String content) {
		spiderLinkDao.updateContent(getMd5Link(link), content, Boolean.TRUE.getCode());
	}

	/**
	 * 根据链接修改是否提取
	 * @param link
	 * @param isFetcherLink
	 */
	public void updateFetcherLink(String link, Integer isFetcherLink) {
		spiderLinkDao.updateFetcherLink(getMd5Link(link), isFetcherLink);
	}

	/**
	 * 修改下载失败次数+1
	 * @param link
	 * @return
	 */
	public int updateDownErrorNum(String link) {
		String md5Link = getMd5Link(link);
		spiderLinkDao.updateDownErrorNum(md5Link);
		Integer num = spiderLinkDao.getDownErrorNum(md5Link);
		return num == null ? 0 : num;
	}

	/**
	 * 获取待提取内容的链接
	 * @param downLinkErrorMaxNum 
	 * @return
	 */
	public String getWaitFetcherContent(int downLinkErrorMaxNum) {
		//return spiderLinkDao.getWaitFetcherContent(Constant.serviceStartTime, downLinkErrorMaxNum);
		return spiderLinkDao.getWaitFetcherContent(downLinkErrorMaxNum);
	}

	/**
	 * 将该链接修改为待提取内容
	 * @param link
	 */
	public void updateWaitFetcherContent(String link) {
		spiderLinkDao.updateWaitFetcherContent(getMd5Link(link));
	}

	/**
	 * 修改提取状态为成功
	 * @param link
	 */
	public void updateDbSuccIsFetcherContent(String link) {
		spiderLinkDao.updateDbSuccIsFetcherContent(getMd5Link(link));
	}

	public ResponseFrame pageQuery(SpiderLink spiderLink) {
		spiderLink.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = spiderLinkDao.findSpiderLinkCount(spiderLink);
		List<SpiderLink> data = null;
		if(total > 0) {
			data = spiderLinkDao.findSpiderLink(spiderLink);
		}
		Page<SpiderLink> page = new Page<SpiderLink>(spiderLink.getPage(), spiderLink.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	public void delete(Integer linkId) {
		spiderLinkDao.delete(linkId);
	}

	public void deleteBatch(Integer siteId, Integer isFetcherContent,
			Integer isFetcherLink) {
		spiderLinkDao.deleteBatch(siteId, isFetcherContent, isFetcherLink);
	}
}